package com.lemon.smartsafetynew.ui.fragment.hidden;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lemon.android.http.LemonRequestParams;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.Constants;
import com.lemon.smartsafety.entity.event.http.HttpCallbackEvent;
import com.lemon.smartsafety.http.callback.HandlerCallback;
import com.lemon.smartsafetynew.ui.fragment.base.BaseFragment;
import com.lemon.smartsafety.utils.app.HUDUtil;
import com.lemon.smartsafetynew.entity.common.DateTypeEnum;
import com.lemon.smartsafetynew.entity.event.hidden.ClickEvent;
import com.lemon.smartsafetynew.entity.hidden.SafeHiddenBo;
import com.lemon.smartsafetynew.entity.hidden.SafeHiddenDownloadVo;
import com.lemon.smartsafetynew.net.services.SafeHiddenServices;
import com.lemon.smartsafetynew.ui.adapter.hidden.SafeHiddenAttachmentAdapter;
import com.lemon.smartsafetynew.util.common.CommonUtil;
import com.lemon.smartsafetynew.util.constants.KeyConstants;
import com.lemon.smartsafetynew.util.hidden.SafeHiddenUtils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * Created by Andrew on 2017/9/11.
 */

public class SafeHiddenProcessTabOneContentFragment extends BaseFragment {

    @Bind(R.id.hidden_one_label_tv)
    TextView hiddenOneLabelTv;
    @Bind(R.id.hidden_one_tv)
    TextView hiddenOneTv;
    @Bind(R.id.hidden_one_layout)
    LinearLayout hiddenOneLayout;

    @Bind(R.id.hidden_two_label_tv)
    TextView hiddenTwoLabelTv;
    @Bind(R.id.hidden_two_tv)
    TextView hiddenTwoTv;
    @Bind(R.id.hidden_two_layout)
    LinearLayout hiddenTwoLayout;

    @Bind(R.id.hidden_three_label_tv)
    TextView hiddenThreeLabelTv;
    @Bind(R.id.hidden_three_tv)
    TextView hiddenThreeTv;
    @Bind(R.id.hidden_three_layout)
    LinearLayout hiddenThreeLayout;

    @Bind(R.id.hidden_four_label_tv)
    TextView hiddenFourLabelTv;
    @Bind(R.id.hidden_four_tv)
    TextView hiddenFourTv;
    @Bind(R.id.hidden_four_layout)
    LinearLayout hiddenFourLayout;

    @Bind(R.id.hidden_five_label_tv)
    TextView hiddenFiveLabelTv;
    @Bind(R.id.hidden_five_tv)
    TextView hiddenFiveTv;
    @Bind(R.id.hidden_five_layout)
    LinearLayout hiddenFiveLayout;

    // ==================================
    @Bind(R.id.label_one_tv)
    TextView labelOneTv;
    @Bind(R.id.name_one_tv)
    TextView nameOneTv;
    @Bind(R.id.layout_one)
    LinearLayout layoutOne;

    @Bind(R.id.label_two_tv)
    TextView labelTwoTv;
    @Bind(R.id.name_two_tv)
    TextView nameTwoTv;
    @Bind(R.id.layout_two)
    LinearLayout layoutTwo;

    @Bind(R.id.label_three_tv)
    TextView labelThreeTv;
    @Bind(R.id.name_three_tv)
    TextView nameThreeTv;
    @Bind(R.id.layout_three)
    LinearLayout layoutThree;

    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;


    private static SafeHiddenDownloadVo safeHiddenDownloadVo;

    @Override
    protected void getBaseArguments() {
        Bundle bundle = getArguments();
        if (bundle != null) {
            if (bundle.containsKey(KeyConstants.KEY_SAFE_HIDDEN_DOWNLOAD_VO)) {
                safeHiddenDownloadVo = (SafeHiddenDownloadVo) bundle.getSerializable(KeyConstants.KEY_SAFE_HIDDEN_DOWNLOAD_VO);
            }
        }
    }

    public static SafeHiddenProcessTabOneContentFragment newInstance(SafeHiddenDownloadVo data) {

        Bundle args = new Bundle();
        args.putSerializable(KeyConstants.KEY_SAFE_HIDDEN_DOWNLOAD_VO, data);

        SafeHiddenProcessTabOneContentFragment fragment = new SafeHiddenProcessTabOneContentFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    protected int getContentViewId() {
        return R.layout.fragment_safe_hidden_tab_one_content;
    }

    @Override
    protected void initViews() {

        initCommon();
        initRecyclerView();

    }

    private void initCommon() {
        //disable by Andrew.yin 2017/10/24 begin
//        if (SafeHiddenBo.Status.rectify.getValue().equals(safeHiddenDownloadVo.getStatus())) {
//            create();
//        } else if (SafeHiddenBo.Status.review.getValue().equals(safeHiddenDownloadVo.getStatus())) {
//            rectify();
//        } else if (SafeHiddenBo.Status.finish.getValue().equals(safeHiddenDownloadVo.getStatus())) {
//            review();
//        }
        //disable by Andrew.yin 2017/10/24 end
        create(); //只显示发起信息
    }

    /**
     * 发起
     */
    private void create() {

        hiddenThreeLayout.setVisibility(View.VISIBLE);
        hiddenFourLayout.setVisibility(View.VISIBLE);
        hiddenFiveLayout.setVisibility(View.VISIBLE);
        layoutThree.setVisibility(View.VISIBLE);

        hiddenOneLabelTv.setText("隐患来源：");
        hiddenOneTv.setText(CommonUtil.avoidNullString(SafeHiddenUtils.getSourceType(safeHiddenDownloadVo.getSource_type())));

        hiddenTwoLabelTv.setText("检查部位：");
        hiddenTwoTv.setText(CommonUtil.avoidNullString(safeHiddenDownloadVo.getName(), ""));

        hiddenThreeLabelTv.setText("检查内容：");
        hiddenThreeTv.setText(CommonUtil.avoidNullString(safeHiddenDownloadVo.getFlow_name()));

        hiddenFourLabelTv.setText("检查时间：");
        hiddenFourTv.setText(CommonUtil.secondToTimeStr(safeHiddenDownloadVo.getReport_time(), DateTypeEnum.DAY));

        hiddenFiveLabelTv.setText("检查人员：");
        hiddenFiveTv.setText(CommonUtil.avoidNullString(safeHiddenDownloadVo.getReport_user_name()));

        layoutOne.setVisibility(View.GONE);
        labelOneTv.setText("隐患分类：");
        nameOneTv.setText(CommonUtil.avoidNullString(safeHiddenDownloadVo.getClassify_name()));

        labelTwoTv.setText("隐患级别：");
        nameTwoTv.setText(CommonUtil.avoidNullString(safeHiddenDownloadVo.getLevel_name()));

        labelThreeTv.setText("隐患描述：");
        nameThreeTv.setText(CommonUtil.avoidNullString(safeHiddenDownloadVo.getDesc()));

        getAttachment("1");
    }

    /**
     * 整改
     */
    private void rectify() {

        hiddenThreeLayout.setVisibility(View.VISIBLE);
        hiddenFourLayout.setVisibility(View.VISIBLE);
        hiddenFiveLayout.setVisibility(View.GONE);
        layoutThree.setVisibility(View.VISIBLE);


        hiddenOneLabelTv.setText("责任部门：");
        hiddenOneTv.setText(CommonUtil.avoidNullString(safeHiddenDownloadVo.getRectify_dept()));

        hiddenTwoLabelTv.setText("责 任 人：");
        hiddenTwoTv.setText(CommonUtil.avoidNullString(safeHiddenDownloadVo.getRectify_user_name()));

        hiddenThreeLabelTv.setText("计划完成时间：");
        hiddenThreeTv.setText(CommonUtil.secondToTimeStr(safeHiddenDownloadVo.getPlan_rectify_time(), DateTypeEnum.DAY));

        hiddenFourLabelTv.setText("预计整改金额：");
        hiddenFourTv.setText(CommonUtil.avoidNullString(safeHiddenDownloadVo.getPlan_rectify_money()));


        labelOneTv.setText("实际整改时间：");
        nameOneTv.setText(CommonUtil.secondToTimeStr(safeHiddenDownloadVo.getReal_rectify_time(), DateTypeEnum.DAY));

        labelTwoTv.setText("实际整改金额：");
        nameTwoTv.setText(CommonUtil.avoidNullString(safeHiddenDownloadVo.getReal_rectify_money()));

        labelThreeTv.setText("整改措施：");
        nameThreeTv.setText(CommonUtil.avoidNullString(safeHiddenDownloadVo.getRectify_desc()));

        getAttachment("2");
    }

    /**
     * 复查
     */
    private void review() {

        hiddenThreeLayout.setVisibility(View.GONE);
        hiddenFourLayout.setVisibility(View.GONE);
        hiddenFiveLayout.setVisibility(View.GONE);
        layoutThree.setVisibility(View.GONE);

        hiddenOneLabelTv.setText("复查部门：");
        hiddenOneTv.setText(CommonUtil.avoidNullString(safeHiddenDownloadVo.getReview_dept()));

        hiddenTwoLabelTv.setText("复查人员：");
        hiddenTwoTv.setText(CommonUtil.avoidNullString(safeHiddenDownloadVo.getReview_user_name()));

        labelOneTv.setText("复查时间：");
        nameOneTv.setText(CommonUtil.secondToTimeStr(safeHiddenDownloadVo.getReview_time(), DateTypeEnum.DAY));

        labelTwoTv.setText("备注说明：");
        nameTwoTv.setText(CommonUtil.avoidNullString(safeHiddenDownloadVo.getReview_remark()));

        getAttachment("3");

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onClickEvent(ClickEvent clickEvent) {

        if (SafeHiddenBo.Status.create.getDesc().equals(clickEvent.getType())) {
            create();
        } else if (SafeHiddenBo.Status.rectify.getDesc().equals(clickEvent.getType())) {
            rectify();
        } else if (SafeHiddenBo.Status.review.getDesc().equals(clickEvent.getType())) {
            review();
        }

    }

    /**
     * 附件
     */
    private SafeHiddenAttachmentAdapter mSafeHiddenAttachmentAdapter;
    private List<String> attests = new ArrayList<>();

    private void initRecyclerView() {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getmContext(), 3);
        recyclerView.setLayoutManager(gridLayoutManager);
        recyclerView.setNestedScrollingEnabled(false);
        mSafeHiddenAttachmentAdapter = new SafeHiddenAttachmentAdapter(attests, SafeHiddenProcessTabOneContentFragment.class.getName());
        recyclerView.setAdapter(mSafeHiddenAttachmentAdapter);
    }


    /**
     * 获取附件
     */
    private void getAttachment(String type) {

        LemonRequestParams params = new LemonRequestParams("");
        params.put("danger_id", safeHiddenDownloadVo.getId());
        params.put("type", type);

        SafeHiddenServices
                .init()
                .getHiddenAttachment(params);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(HttpCallbackEvent event) {
        if (KeyConstants.URL_GET_SAFE_HIDDEN_ATTACHMENT.equals(event.getTag())) {                    //获取附件
            if (HandlerCallback.HttpFlow.START == event.getHttpFlow()) {
                mHUDUtil.showHUD();
            } else if (HandlerCallback.HttpFlow.FINISH == event.getHttpFlow()) {
                mHUDUtil.hidden();
                if (Constants.Result.SUCCESS == event.getEventResult()) {

                    if (mSafeHiddenAttachmentAdapter != null) {
                        attests.clear();
                        mSafeHiddenAttachmentAdapter.notifyDataSetChanged();
                        mSafeHiddenAttachmentAdapter.addItems((List<String>)event.getObj());
                    }

                } else if (Constants.Result.ERROR == event.getEventResult()) {

                    mHUDUtil.showErrorTip(event.getMsg(), new HUDUtil.CallBack() {
                        @Override
                        public void onCall() {

                        }
                    });

                }
            }
        }
    }
}
